<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入需求标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核类型" prop="keyword">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择审核类型"
          clearable
        >
          <el-option :key="0" label="认证审核" :value="0"></el-option>
          <el-option :key="1" label="资源供给审核" :value="1"></el-option>
          <el-option :key="2" label="资讯审核" :value="2"></el-option>
          <el-option :key="3" label="通知审核" :value="3"></el-option>
          <el-option :key="4" label="元素审核" :value="4"></el-option>
          <el-option :key="5" label="资源节点审核" :value="5"></el-option>
          <el-option :key="6" label="申请资源" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option :key="1" label="待审核" :value="1"></el-option>
          <el-option :key="2" label="通过" :value="2"></el-option>
          <el-option :key="3" label="驳回" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="reviewList"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          {{ row.status === 1 ? "待审核" : row.status === 2 ? "通过" : "驳回" }}
        </template>
      </el-table-column>
      <el-table-column label="审核类型" align="center" prop="type">
        <template slot-scope="{row}">
          {{
            row.type === 0
              ? "认证审核"
              : row.type === 1
              ? "资源供给审核"
              : row.type === 2
              ? "资讯审核"
              : row.type === 3
              ? "通知审核"
              : row.type === 4
              ? "元素审核"
              : row.type === 5
              ? "资源节点审核"
              : row.type === 6
              ? "申请资源" : "未知类型"
          }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="审核人" align="center" prop="auditUserName" />
      <el-table-column label="审核标题" align="center" prop="auditTitle" />
      <el-table-column label="审核时间" align="center" prop="auditTime" />
      <el-table-column label="审核反馈" align="center" prop="auditResult" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="doDetail(scope.row)">详情</el-button>
          <el-button
            type="text"
            v-if="scope.row.status === 1"
            @click="doReview(scope.row.id)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="审核"
         :close-on-click-modal="false"
      :visible.sync="visible"
      width="500px"
      append-to-body
      @close="reviewClose"
    >
      <el-form
        ref="reviewForm"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核说明" prop="auditResult">
          <el-input
            type="textarea"
            v-model="formData.auditResult"
            placeholder="请输入审核说明"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="confirm" :loading="confirmLoading"
          >确定</el-button
        >
        <el-button @click="visible = false">取消</el-button>
      </template>
    </el-dialog>
    <DetailDrawer
      :visible.sync="showDrawer"
      :id="currRow.associateId"
      :type="currRow.type"
    />
  </div>
</template>

<script>
import { reviewList, doReview } from "@/api/system/review";
import DetailDrawer from "./components/detailDrawer.vue";

export default {
  name: "ContentReview",
  components: { DetailDrawer },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求信息表格数据
      reviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
        type: null,
        status: null,
      },
      currId: "",
      visible: false,
      formData: {
        status: null,
        auditResult: "",
      },
      rules: {
        status: { required: true, message: "请选择审核结果" },
        auditResult: { required: true, message: "请输入审核说明" },
      },
      confirmLoading: false,
      showDrawer: false,
      currRow: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    doDetail(row) {
      this.currRow = row;
      this.$nextTick(() => {
        this.showDrawer = true;
      });
    },
    confirm() {
      this.$refs.reviewForm.validate(async (valid) => {
        if (valid) {
          this.confirmLoading = true;

          try {
            await doReview({
              id: this.currId,
              ...this.formData,
            });
            this.$message.success("审核成功");
            this.visible = false;
            this.getList();
          } finally {
            this.confirmLoading = false;
          }
        }
      });
    },
    reviewClose() {
      this.$refs.reviewForm.resetFields();
    },
    doReview(id) {
      this.currId = id;
      this.visible = true;
    },
    /** 查询需求信息列表 */
    getList() {
      this.loading = true;
      reviewList(this.queryParams).then((response) => {
        this.reviewList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.demandId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
};
</script>
